package com.example.travelapplication.activity;

import androidx.annotation.NonNull;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.SearchView;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;
import androidx.room.Room;

import android.content.Intent;
import android.os.Bundle;
import android.view.MenuItem;

import com.example.travelapplication.R;
import com.example.travelapplication.adapter.AttractionAdapter;
import com.example.travelapplication.dao.AttDao;
import com.example.travelapplication.database.TravelDatabase;
import com.example.travelapplication.entity.Attraction;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class MoreActivity extends AppCompatActivity {

    private AttractionAdapter adapter;
    private List<Attraction> list = new ArrayList<>();
    private RecyclerView recyclerView;
    private SearchView searchView;
    private TravelDatabase travelDatabase;
    private AttDao attDao;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_more);

        ActionBar actionBar = getSupportActionBar();
        actionBar.setDisplayHomeAsUpEnabled(true);
        actionBar.setTitle("更多景点");
        init();
    }

    private void init(){
        travelDatabase = Room.databaseBuilder(MoreActivity.this, TravelDatabase.class, "travelDb")
                .allowMainThreadQueries()
                .build();
        attDao = travelDatabase.attDao();
        list = attDao.getAllAtt();
        Collections.shuffle(list); // 打乱 list 的顺序

        adapter = new AttractionAdapter(list, MoreActivity.this, new AttractionAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(int position) {
                Intent intent = new Intent(MoreActivity.this, DetailActivity.class);
                intent.putExtra("id", list.get(position).attId);
                MoreActivity.this.startActivity(intent);
            }
        });
        recyclerView = (RecyclerView) findViewById(R.id.rv_more);
        recyclerView.setLayoutManager(new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL));
        recyclerView.setAdapter(adapter);

        searchView = (SearchView) findViewById(R.id.searchView);
        searchView.setOnQueryTextListener(listener1);
        searchView.setOnCloseListener(listener2);
    }

    SearchView.OnQueryTextListener listener1 = new SearchView.OnQueryTextListener() {
        @Override
        public boolean onQueryTextSubmit(String query) {
            list = attDao.getAttBySearch(query);
            adapter.setData(list);
            adapter.notifyDataSetChanged();
            return false;
        }

        @Override
        public boolean onQueryTextChange(String newText) {
            return false;
        }
    };

    SearchView.OnCloseListener listener2 = new SearchView.OnCloseListener() {
        @Override
        public boolean onClose() {
            list = attDao.getAllAtt();
            adapter.setData(list);
            adapter.notifyDataSetChanged();
            return false;
        }
    };

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                finish();
                break;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        travelDatabase.close(); //关闭数据库
    }
}